//
//  SPCHomeViewController.m
//  SmallPigCar
//
//  Created by 张令林 on 2016/11/1.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "SPCHomeViewController.h"
#import "UINavigationBar+NavAlpha.h"
#import "SPCTestViewController.h"

static NSString *cellid = @"cellid";
static NSString *headerid = @"headerid";


@interface SPCHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,weak) UIView *backView;
@property (nonatomic,strong) UIColor *color;
@property (nonatomic,weak) UICollectionView *collectionView;
@property (nonatomic,weak) UIImageView *imageView;





@end

@implementation SPCHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //调用初始化方法
    [self setUpUI];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = true;
    //设置上部图片的布局
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.collectionView.mas_top);
        make.centerX.equalTo(self.collectionView.mas_centerX);
    }];
    //设置collection的布局
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(-20);
        make.bottom.left.right.offset(0);
    }];

    
}
-(void)viewDidAppear:(BOOL)animated
{
}
#pragma mark 初始化方法
- (void)setUpUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    //设置导航视图
    UIColor *color = [UIColor colorWithRed:110 green:203 blue:108];
    self.color = color;
    //设置顶部的定位和搜索视图
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 70)];
    [self.view insertSubview:backView aboveSubview:self.collectionView];
    self.backView = backView;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, 30)];
    topView.backgroundColor = [UIColor whiteColor];
    topView.layer.cornerRadius = 5;
//    self.navigationItem.titleView = topView;
    [self.backView addSubview:topView];
    UIButton *getLocationBtn = [[UIButton alloc] initWithFrame:CGRectMake(3, 0, 70, 30)];
    getLocationBtn.backgroundColor = [UIColor whiteColor];
    [topView addSubview:getLocationBtn];
    UIView *centerLine = [[UIView alloc] initWithFrame:CGRectMake(70, 0, 1, 30)];
    centerLine.backgroundColor = [UIColor colorWithRed:230 green:230 blue:230];
    [topView addSubview:centerLine];
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(71, 0, 277, 30)];
    searchBtn.backgroundColor = [UIColor whiteColor];
    [topView addSubview:searchBtn];
    //添加collection
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor colorWithRed:222 green:222 blue:222];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    //注册单元格
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellid];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerid];
    //添加collection上面的视图
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"esc_drop_back"]];
    [self.collectionView addSubview:imageView];
    self.imageView = imageView;
}

#pragma mark - collection的数据源方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexPath];
    cell.backgroundColor = [UIColor randomColor];
    return cell;
}

//设置不同cell的size
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.section == 0)
    {  //第0组
        return CGSizeMake((collectionView.bounds.size.width - 3)/4, 50);
    }
    if (indexPath.section == 1)
    {  //第0组
        return CGSizeMake((collectionView.bounds.size.width - 1)/2, 50);
    }
    return CGSizeMake(collectionView.bounds.size.width , 100);
}

//组头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqual:UICollectionElementKindSectionFooter])
    {
        return nil;
    }
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerid forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor grayColor];
    return headerView;
}

//返回不同组的组头高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return CGSizeMake(self.view.frame.size.width, 278);//250
    }else
    {
        return CGSizeMake(self.view.bounds.size.width, 30);
    }
}
#pragma mark - collection的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offSetY = scrollView.contentOffset.y;
    NSLog(@"%lf",offSetY);
    CGFloat alpha = 1+offSetY/20;
    if (alpha > 0.6)
    {
        alpha = 0.6;
    }
    if (alpha <= 0)
    {
        alpha = 0;
    }
    self.backView.backgroundColor = [self.color colorWithAlphaComponent:alpha];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SPCTestViewController *vc = [SPCTestViewController new];
    self.navigationController.navigationBar.hidden = false;
    [self.navigationController pushViewController:vc animated:YES];
    
}


@end
